﻿using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Company.StudyClub.DAL;
using Company.StudyClub.Model;

namespace Company.StudyClub.BLL.Site
{
    public class AreaLayer
    {
        private static AreaLayer instance = null;
        private AreaLayer()
        {
            this.Init();
        }
        private static readonly object padlock = new object();
        public static AreaLayer Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (padlock)
                    {
                        instance = new AreaLayer();
                    }
                }
                return instance;
            }
        }

        private void Init()
        {
            DAL.Site.AreaDAL objA = new DAL.Site.AreaDAL();
            List<Site_Area> areaMappingList = objA.QueryArea(ConfigurationManager.AppSettings.Get("ApplicationName"));
            // Push to Cache
            Cache.CacheManager.Insert(areaMappingList, Cache.CacheManager.AREA_MAP_KEY, TimeSpan.FromMinutes(Cache.CacheManager.AREA_MAP_REFRESH_LENGTH));
            this.lastRefreshedDate = DateTime.Now;
        }

        public string GetAreaName()
        {
            HttpContext objContext = HttpContext.Current;
            return GetAreaName(objContext.Request.ServerVariables["SCRIPT_NAME"].Trim());
        }

        public string GetAreaName(string scriptName)
        {
            scriptName = scriptName.Trim().ToLower();

            string areaName = this.GetAreaMap(scriptName);
            if (areaName != "UNKNOWN")
            {
                return areaName;
            }

            if (scriptName.LastIndexOf("/") >= 0)
            {
                scriptName = scriptName.Substring(0, scriptName.LastIndexOf("/")) + "/";
            }

            while (scriptName.LastIndexOf("/") >= 0)
            {
                areaName = this.GetAreaMap(scriptName);
                if (areaName != "UNKNOWN")
                {
                    break;
                }
                scriptName = scriptName.Substring(0, scriptName.Length - 1);
                scriptName = scriptName.Substring(0, scriptName.LastIndexOf("/")) + "/";
            }

            return areaName;
        }

        private string GetAreaMap(string scriptName)
        {
            List<Site_Area> mapList = this.AreaMappingList;
            if (mapList == null)
                return "UNKNOWN";
            for (int i = 0; i < mapList.Count; i++)
            {
                Site_Area objMap = (Site_Area)mapList[i];
                if (scriptName == objMap.ScriptName.Trim().ToLower())
                {
                    return objMap.AreaName;
                }
            }
            return "UNKNOWN";
        }

        private List<Site_Area> AreaMappingList
        {
            get
            {
                if (Cache.CacheManager.Get(Cache.CacheManager.AREA_MAP_KEY) == null)
                    this.Init();
                return (List<Site_Area>)Cache.CacheManager.Get(Cache.CacheManager.AREA_MAP_KEY);
            }
        }

        public void Refresh()
        {
            try
            {
                this.Init();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private DateTime lastRefreshedDate;
        public DateTime LastRefreshedDate
        {
            get { return this.lastRefreshedDate; }
            set { this.lastRefreshedDate = value; }
        }
    }
}
